package com.feiyuan.core.controller.admin;

import com.feiyuan.common.result.R;
import com.feiyuan.core.annotation.FeiyuanRequiresPermissions;
import com.feiyuan.core.pojo.entity.AdminUser;
import com.feiyuan.core.service.AdminUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 后台管理员
 * @author Xupf
 */
@Api(tags = "后台管理员管理")
@RestController
@RequestMapping("/admin/core/adminUser")
@Slf4j
//@CrossOrigin
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @ApiOperation("查询所有的管理员信息")
    @GetMapping("/list")
    @FeiyuanRequiresPermissions("admin:user:list")
    public R list(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize,
                  @RequestParam("userName") String userName, @RequestParam("name") String name, @RequestParam("phone") String phone, @RequestParam("email") String email) {
        IPage<AdminUser> pages = adminUserService.listPage(pageNo, pageSize, userName, name, phone, email);
        return R.ok().data("pages", pages);
    }

    @ApiOperation("查询单个信息")
    @GetMapping("/info/{id}")
    @FeiyuanRequiresPermissions("admin:user:info")
    public R info(@PathVariable Long id) {
        return R.ok().data("info", adminUserService.getByUserId(id));
    }

    @ApiOperation("保存或者修改管理员信息")
    @PostMapping("/saveOrUpdate")
    @FeiyuanRequiresPermissions("admin:user:saveOrUpdate")
    public R saveOrUpdate(
            @ApiParam(value = "管理员表对象", required = true)
            @RequestBody AdminUser adminUser
    ) {
        boolean b = adminUserService.saveOrUpdateUser(adminUser);
        if (b) {
            return R.ok().message("更新成功");
        } else {
            return R.error().message("更新失败");
        }
    }

    @ApiOperation("删除管理员信息")
    @PostMapping("/remove/{id}")
    @FeiyuanRequiresPermissions("admin:user:remove")
    public R remove(@PathVariable Long id) {
        boolean b = adminUserService.removeById(id);
        if (b) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }
}
